﻿#include "stdafx.cuh"
#include "cuPrintfMain.cuh"
#include "SweepCircleVoronoi.cu"
#include "EventQueue.cu"
#include "VoronoiFunction.cu"

//graphics functions
void SetPosition();
void display();
void init();
void DrawMyself();

void SetPosition()
{
	glPolygonMode(GL_BACK,GL_LINE);
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
	glTranslatef(0.0f,0.0f,-1.5f);
	glTranslatef(0.0f,0.0f,1.5f);
}

void display()
{
     /*clear window */
	glClearColor(1,1,1,1);
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    /*draw unit square polygon*/
	SetPosition();
	DrawMyself();
	glutSwapBuffers();
}

void ReSize(int cx,int cy)
{
	glViewport(0,0,cx,cy);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	double scale = 1.5;
	if(cx>cy){
		MinPoint.x = -float(cx)/float(cy)*scale;
		MaxPoint.x = -MinPoint.x;
		MinPoint.y = -scale;
		MaxPoint.y = scale;
		glOrtho(MinPoint.x,MaxPoint.x,MinPoint.y,MaxPoint.y,-10000,10000);
	}else{
		MinPoint.x=-scale;
		MaxPoint.x=scale;
		MinPoint.y=-float(cy)/float(cx)*scale;
		MaxPoint.y=-MinPoint.y;
		glOrtho(MinPoint.x,MaxPoint.x,MinPoint.y,MaxPoint.y,-10000,10000);
	}
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
}

void DrawMyself()
{
	glPushAttrib(GL_ENABLE_BIT|GL_POLYGON_BIT|GL_LINE_BIT);
	glDisable(GL_LIGHTING);
	glPointSize(5);
	glBegin(GL_POINTS);
	
	//printf("h_sites draw %d\n" , h_sitesLength[0]);
	//printf("sites %d",sites.size());
	for(int i = 0; i <h_sitesLength[0];++i){
		if((h_sites[i].x-h_center[0].x)*(h_sites[i].x-h_center[0].x)+(h_sites[i].y-h_center[0].y)*(h_sites[i].y-h_center[0].y)<distance*distance){
			glColor3f(0.5f,0.f,0.5f);
		}else{
			glColor3f(0.7f,0.7f,0.5f);
		}
		glVertex2d((float)h_sites[i].x,(float)h_sites[i].y);
		//printf("%f %f\n",sites[i].x ,sites[i].y);
	}
	glEnd();
	glColor3f(0, 1, 0);
	glBegin(GL_POINTS);
	glVertex2f((float)h_center[0].x, (float)h_center[0].y);
	glEnd();
	glPopAttrib();
}



void init()
{
      glClearColor(0.0, 0.0, 0.0, 0.0);
	  glClearDepth(10000.0);


	  initCUDA();
}

void clean()
{
	cudaHostUnregister(h_sites);
	cudaHostUnregister(h_sitesLength);
	VirtualFree(h_sites,0,MEM_RELEASE);
	VirtualFree(h_sitesLength,0,MEM_RELEASE);

	if( h_vertexPoolLength != NULL )
		free( h_vertexPoolLength );
	if( h_vertexPool != NULL )
		free( h_vertexPool );
	if( h_edgePoolLength != NULL )
		free( h_edgePoolLength );
	if( h_edgePool != NULL )
		free( h_edgePool );
	if( h_center != NULL )
		free( h_center );
	if( h_arcsPool != NULL )
		free( h_arcsPool );
	if( h_arcsPoolLength != NULL )
		free( h_arcsPoolLength );

	if( d_sitesLength != NULL )
		cudaFree( d_sitesLength );
	if( d_sites != NULL )
		cudaFree( d_sites );
	if( d_vertexPool != NULL )
		cudaFree( d_vertexPool );
	if( d_vertexPoolLength != NULL )
		cudaFree( d_vertexPoolLength );
	if( d_edgePool != NULL )
		cudaFree( d_edgePool );
	if( d_edgePoolLength != NULL )
		cudaFree( d_edgePoolLength );
	if( d_eventHeap != NULL )
		cudaFree(d_eventHeap);
	if( d_center != NULL )
		cudaFree( d_center );
	if( d_arcHeap != NULL )
		cudaFree(d_arcHeap); 
	if( d_arcsPool != NULL )
		cudaFree( d_arcsPool );
	if( d_arcsPoolLength != NULL )
		cudaFree( d_arcsPoolLength );
	if( d_activeEdgePool != NULL )
		cudaFree( d_activeEdgePool);
	if( d_activeEdgePoolLength != NULL )
		cudaFree( d_activeEdgePoolLength);
	cutilDeviceReset();
}

void keyboardFunc(unsigned char key,int x , int y)
{
	switch(key){
	case 27:
		clean();
		exit(0);
		break;
	}

}

 int main(int argc, char** argv)
 {
      /*initialize mode and open window in upper-left corner of
      screen*/
      /*window title is name of program (arg[0])*/
        glutInit(&argc, argv);
        glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB);
        glutInitWindowSize(500, 500);
        glutInitWindowPosition(300, 300);
        glutCreateWindow("sweepcircle");
        glutDisplayFunc(display);
		glutIdleFunc(display);
		glutKeyboardFunc(keyboardFunc);
	    glutReshapeFunc(ReSize);
        init();

		//initCUDA(argc,argv);
		glutMainLoop();

		
    }